package com.qinggeng.project.api.utils;


import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.spring.SpringUtils;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.impl.OrdersServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrdersCheckUtils {

    @Autowired
    private IOrdersService ordersService;

    /**
     * 根据订单ID检验订单状态
     * @param ordersId
     * @return
     */
    public  Orders checkOrderStatus(Long ordersId){
        Orders orders=ordersService.selectOrdersById(ordersId);
        checkOrderStatus(orders);
        return orders;
    }

    /**
     * 根据订单Id和用户Id检验订单状态
     * @param ordersId
     * @param UserId
     * @return
     */
    public Orders checkOrderStatus(Long ordersId, Long UserId){
        Orders orders=ordersService.selectOrdersByIdAndUserId(ordersId,UserId);
        checkOrderStatus(orders);
        return orders;
    }

    /**
     * 根据订单编号检验订单状态
     * @param ordersNo
     * @return
     */
    public Orders checkOrderStatus(String ordersNo){
        Orders orders=ordersService.selectOrdersByOrdersNo(ordersNo);
        checkOrderStatus(orders);
        return orders;
    }

    /**
     * 检验订单状态
     * @param orders
     * @return
     */
    public boolean checkOrderStatus(Orders orders) {
        if (orders == null) {
            log.error("订单不存在");
            throw new CustomException("订单不存在");
        }
        //订单状态不等于10=进行中
        if (!orders.getStatus().equals(CommonEnum.STATUS_10.getValue())) {
            log.error("订单状态异常");
            throw new CustomException("订单状态异常");
        }
        return true;
    }


    /**
     * 根据订单ID检验订单支付状态
     * @param ordersId
     * @return
     */
    public Orders checkOrdersPayStatus(Long ordersId){
        Orders orders=ordersService.selectOrdersById(ordersId);
        checkOrdersPayStatus(orders);
        return orders;
    }

    /**
     * 根据订单Id和用户Id检验订单支付状态
     * @param ordersId
     * @param userId
     * @return
     */
    public Orders checkOrdersPayStatus(Long ordersId,Long userId){
        Orders orders=ordersService.selectOrdersByIdAndUserId(ordersId,userId);
        checkOrdersPayStatus(orders);
        return orders;
    }



    /**
     * 根据订单编号检验订单支付状态
     * @param ordersNo
     * @return
     */
    public Orders checkOrderPayStatus(String ordersNo){
        Orders orders=ordersService.selectOrdersByOrdersNo(ordersNo);
        checkOrdersPayStatus(orders);
        return orders;
    }

    public  boolean checkOrdersPayStatus(Orders orders){
        checkOrderStatus(orders);
        //支付状态等于20=已支付
        if (!orders.getPayStatus().equals(CommonEnum.STATUS_20.getValue())) {
            log.error("订单支付状态异常");
            throw new CustomException("订单支付状态异常");
        }
        return true;
    }


}
